package com.moli.iov.admin.web.controller;

import com.moli.iov.alarm.model.dto.AlarmVehicleDto;
import com.moli.iov.alarm.remote.IAlarmVehicleStatisticsService;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.his.model.dto.VehicleAnalyzeQueDto;
import com.moli.iov.his.remote.HistoryRemoteService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.DateUtil;
import com.moli.iov.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @ClassName VehicleAnalyzeController
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-04-09 下午 1:29
 * @UpdateDate 2019-04-09 下午 1:29
 * @Version 1.0
 */
@RestController
@Slf4j
public class VehicleAnalyzeController {

    @Reference(group = "${service.group}", check = false,timeout = 10000)
    HistoryRemoteService historyRemoteService;

    @Reference(group = "${service.group}", check = false)
    IVehicleBaseRemoteService vehicleBaseRemoteService;

    @Reference(group = "${service.group}", check = false,timeout = 10000)
    private IAlarmVehicleStatisticsService alarmVehicleStatisticsService;

    @PostMapping("/statistics/vehicleAnalyse/query")
    private RestResponse queryVehicleAnalyze(@RequestBody VehicleAnalyzeQueDto queryDto) {

        RestResponse res = historyRemoteService.queryVehicleAnalyze(queryDto);
//        VehicleAnalyzeDto dt = JsonUtils.parse("{\"chargeCount\":4,\"chargeDuration\":20,\"drivingDuration\":\"30\",\"list\":[{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 01:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 02:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 03:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 04:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 05:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 06:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 07:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 08:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 09:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0},{\"chargeState\":4,\"chargeStateString\":\"充电完成\",\"dynamicState\":0,\"dynamicStateString\":\"禁止\",\"extendBatteryVoltage\":14.2,\"extendLeasing\":0,\"extendLeasingString\":\"非租赁\",\"messageDate\":\"2019-09-09 10:00\",\"odo\":40.3,\"physicalKeyState\":1,\"physicalKeyStateString\":\"使能\",\"powerGear\":1,\"powerGearString\":\"acc档\",\"soc\":34,\"speed\":20.0}],\"odoSum\":12.5}", VehicleAnalyzeDto.class);
        return res;
    }


    /**
     * 查询单个车的告警数据
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/statistics/vehicleAnalyse/queryVehicleAlarm")
    private RestResponse queryVehicleAlarmAnalyze(@RequestBody VehicleAnalyzeQueDto queryDto) throws ParseException {
        String vin = queryDto.getQueryValue();
        if ("1".equals(queryDto.getQueryType())) {  //1:代表车牌号
            try {
                //根据车牌号查询vin
                RestResponse<String> numResult = vehicleBaseRemoteService.queryVinByLicense(queryDto.getQueryValue());
                vin = numResult.getData();
            } catch (Exception e) {
                log.error("[getVinByType][{}]", e.getMessage());
                vin = null;
            }
        } else {
            //2:代表车辆vin号码
            vin = queryDto.getQueryValue();
        }

        if (StringUtil.isBlank(vin)) {
            log.error("根据车牌号获取vin失败", vin);
            return RestResponse.failure("查询失败");
        }


        if (queryDto.getBeginTime() != null) {
            String beginStr =
                    DateUtil.format(DateUtil.YEAR_DAY, queryDto.getBeginTime()) + " 00:00:00";
            queryDto.setBeginTime(DateUtil.parser(DateUtil.YEAR_SECOND, beginStr));
        }

        if (queryDto.getEndTime() != null) {
            String endStr = DateUtil.format(DateUtil.YEAR_DAY, queryDto.getEndTime()) + " 23:59:59";
            queryDto.setEndTime(DateUtil.parser(DateUtil.YEAR_SECOND, endStr));
        }

        List<AlarmVehicleDto> alarmVehicleDtos = alarmVehicleStatisticsService.queryVehicleAlarmAnalyze(queryDto.getBeginTime(), queryDto.getEndTime(), vin);
        return RestResponse.success(alarmVehicleDtos);
    }
}
